package io.renren.modules.app.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.AddressVo;
import io.renren.modules.app.entity.UserVo;
import io.renren.modules.app.service.ApiAddressService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收获地址api
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019-7-3 12:27
 */
@Api("收获地址api")
@RestController
@RequestMapping("/app/v1/address")
public class ApiAddressController {
    @Autowired
    private ApiAddressService apiAddressService;

    /**
     * 获取用户的收货地址
     */
    @Login
    @GetMapping("/list")
    public R list(@LoginUser UserVo userVo){
        List<AddressVo> addressList = apiAddressService.list(
                new QueryWrapper<AddressVo>().eq("user_id",userVo.getUserId())
        );
        return R.ok().put("addressList",addressList);
    }

    /**
     * 获取收货地址的详情
     */
    @Login
    @GetMapping("/detail")
    public R detail(Integer id,@LoginUser UserVo userVo){
        AddressVo address = apiAddressService.getOne(
                new QueryWrapper<AddressVo>().eq("id",id).eq("user_id",userVo.getUserId())
        );
        return R.ok().put("address",address);
    }

    /**
     * 添加收货地址
     */
    @Login
    @PostMapping("/save")
    public R save(@LoginUser UserVo userVo,@RequestBody AddressVo address){
        address.setUserId(userVo.getUserId());
        ValidatorUtils.validateEntity(address, AddGroup.class);
        if(address.getIsDefault() == 1){
            List<AddressVo> addressList = apiAddressService.list(
                new QueryWrapper<AddressVo>().eq("user_id",userVo.getUserId())
            );
            if(!StringUtil.isNullOrEmpty(addressList)){
                for(AddressVo addressVo : addressList){
                    addressVo.setIsDefault(0);
                    apiAddressService.updateById(addressVo);
                }
            }
        }
        List<AddressVo> addressArray = apiAddressService.list(
                new QueryWrapper<AddressVo>()
                        .eq("user_id",userVo.getUserId())
                        .eq("user_name",address.getUserName())
                        .eq("tel_number",address.getTelNumber())
                        .eq("detail_info",address.getDetailInfo())
                        .eq("name",address.getName())
                        .eq("area",address.getArea())
        );
        if(StringUtil.isNullOrEmpty(addressArray)){
            apiAddressService.save(address);
            return R.ok("添加成功");
        }else{
            return R.error("地址已存在");
        }
    }

    /**
     * 更新收货地址
     */
    @Login
    @PostMapping("/update")
    public R update(@LoginUser UserVo userVo,@RequestBody AddressVo address){
        ValidatorUtils.validateEntity(address, UpdateGroup.class);
        if(address.getIsDefault() == 1){
            List<AddressVo> addressList = apiAddressService.list(
                    new QueryWrapper<AddressVo>().eq("user_id",userVo.getUserId())
            );
            if(!StringUtil.isNullOrEmpty(addressList)){
                for(AddressVo addressVo : addressList){
                    addressVo.setIsDefault(0);
                    apiAddressService.updateById(addressVo);
                }
            }
        }
        List<AddressVo> addressArray = apiAddressService.list(
                new QueryWrapper<AddressVo>()
                        .eq("user_id",userVo.getUserId())
                        .eq("user_name",address.getUserName())
                        .eq("tel_number",address.getTelNumber())
                        .eq("detail_info",address.getDetailInfo())
                        .eq("name",address.getName())
                        .eq("area",address.getArea())
                        .eq("is_default",address.getIsDefault())
        );
        if(StringUtil.isNullOrEmpty(addressArray)){
            apiAddressService.updateById(address);
            return R.ok("修改成功");
        }else{
            return R.error("地址已存在");
        }
    }

    /**
     * 删除收货地址
     */
    @Login
    @PostMapping("/delete")
    public R delete(@RequestBody Integer id,@LoginUser UserVo userVo){
        apiAddressService.remove(
                new QueryWrapper<AddressVo>().eq("id",id).eq("user_id",userVo.getUserId())
        );
        return R.ok();
    }
}
